﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using cbmobile.BLC;
using cbmobile.DAC;
using PQT.API;
using PQT.API.File;
using PQT.Common;
using PQT.Controls;
using Telerik.WebControls;
using PQT.API.DataDefine.Sys;

namespace cbmobile.UserControl
{
    public partial class LinhKien_Entry : CommonUserControl
    {
        BLC_LinhKien blc = new BLC_LinhKien();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["action"] != null)
                {
                    switch (Request.QueryString["action"].ToLower())
                    {
                        case "addnew": frmBlogCate.ChangeMode(FormViewMode.Insert);
                            break;
                    }
                }
            }
        }

        #region upload
        private string uploadimg(FileUpload fileImage)
        {
            //try
            //{
            LinhKien entP = blc.Get_Components_By_ID(this.Ma_LinhKien);
            string filePath = fileImage.FileName;
            if (entP != null)
            {
                if (filePath != "")
                {
                    fileImage.SaveAs(MapPath("~/ImagesSP/" + filePath));

                    return filePath;
                }
                else
                {
                    filePath = entP.HinhAnh;
                    return filePath;
                }


                //fileImage.SaveAs(MapPath("~/ImagesSP/" + filePath));


            }
            else
            {


                fileImage.SaveAs(MapPath("~/ImagesSP/" + filePath));

                return filePath;
            }




            //}
            //catch (System.Exception ex)
            //{
            //    return 0;
            //}

        }
        #endregion

        private DropDownList findcontrol_DropList(string findcontrol)
        {
            return frmBlogCate.FindControl(findcontrol) as DropDownList;
        }


        private void BindControlField()
        {
            LinhKien entP = blc.Get_Components_By_ID(this.Ma_LinhKien);
            DropDownList ddlStatus = frmBlogCate.FindControl("ddlStatus") as DropDownList;
            if (ddlStatus != null)
            {
                ddlStatus.Items.Clear();

                ddlStatus.Items.Add(new ListItem(CommonStatus.Inactive.ToString(), ((int)CommonStatus.Inactive).ToString()));
                ddlStatus.Items.Add(new ListItem(CommonStatus.Active.ToString(), ((int)CommonStatus.Active).ToString()));

                if (entP != null)
                {
                    ddlStatus.SelectedValue = entP.TinhTrang.ToString();
                }
            }
        }

        protected string StatusName(object StatusID)
        {
            return StatusID != null ? ((CommonStatus)StatusID).ToString() : string.Empty;
        }

        protected int Ma_LinhKien
        {
            get
            {
                return Utility.ValidateParam("Ma_LinhKien", 0);
            }
        }

        protected void frmBlogCate_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            Response.Redirect(this.ListLink);
        }

        protected void frmBlogCate_ItemDeleted(object sender, FormViewDeletedEventArgs e)
        {
            Response.Redirect(this.ListLink);
        }

        protected void ObjectDataSource1_Updating(object sender, ObjectDataSourceMethodEventArgs e)
        {
            DropDownList ddlStatus = frmBlogCate.FindControl("ddlStatus") as DropDownList;
            e.InputParameters["TinhTrang"] = ddlStatus.SelectedValue;
            FileUpload fileImage = frmBlogCate.FindControl("fileImage") as FileUpload;
            e.InputParameters["HinhAnh"] = uploadimg(fileImage);
        }

        protected void ObjectDataSource1_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            DropDownList ddlStatus = frmBlogCate.FindControl("ddlStatus") as DropDownList;
            e.InputParameters["TinhTrang"] = ddlStatus.SelectedValue;
            FileUpload fileImage = frmBlogCate.FindControl("fileImage") as FileUpload;
            e.InputParameters["HinhAnh"] = uploadimg(fileImage);
        }

        protected void frmBlogCate_DataBound(object sender, EventArgs e)
        {
            BindControlField();
        }
    }
}